package demo12;

import java.util.*;

public class BinInsert {
    public int binInsert(int n, int m, int j, int i) {
        // write code here
        int num1 = n;
        int num2 = m;
        int[] arr1 = new int[32];
        for(int a = 0; a < 32; a++) {
            arr1[a] = num1 & 1;
            num1 >>= 1;
        }
        boolean flag = false;
        for(int a = j; a <= i; a++) {
            if(arr1[a] == 0) {
                flag = true;
            }
        }
        int[] arr2 = new int[i-j+1];
        for(int a = 0; a< i-j+1; a++) {
            arr2[a] = num2 & 1;
            num2 >>= 1;
        }
        int z = 0;
        if(flag) {
            for(int a = j; a <= i; a++) {
                    arr1[a] = arr2[z];
                    z++;
                }
            }
        int num = 0;
        for(int a = 0; a < 32; a++) {
            if(arr1[a] == 1) {
                num += Math.pow(2,a);
            } 
        }
        return num;
    }

    public static void main(String[] args) {
        BinInsert binInsert = new BinInsert();
        int num = binInsert.binInsert(1024,19,2,6);
        System.out.println(num);
    }
}